﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text;
using System.IO;
using NeurOthello.NNetwork;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


namespace NeurOthello.Logic
{
    public class NNHeuristic : Heuristic
    {

        Network network = new Network(128, 2, new int[1] { 40 });
        Random rand = new Random();

        public NNHeuristic() 
        {
            LoadNetwork("network500000.nnt");
        }

        public NNHeuristic(string filename)
        {
            LoadNetwork(filename);
        }

        public NNHeuristic(Network network)
        {
            this.network = network;
        }

        public override double Weights(Othello o, int playerID)
        {
            double[] res = network.ProcessInput(o.Inputboard);

            return res[playerID - 1] - res[2 - playerID] + rand.NextDouble() * 0.01;
        }



        public void LoadNetwork(string filename)
        {
            Stream stream = File.Open(filename, FileMode.Open);
            BinaryFormatter bformatter = new BinaryFormatter();

            Console.WriteLine("Reading NN Information");
            network = (Network)bformatter.Deserialize(stream);
            stream.Close();
        }
    }
}
